<?php
require_once APPLICATION_PATH.'\models\Search.php';
require_once APPLICATION_PATH.'\models\Users.php';
require_once APPLICATION_PATH.'\models\Home.php';
require_once APPLICATION_PATH.'\models\PublishSubscribe.php';


/**
 * This controller deals with the engine allowing the current user to search for
 * other users (= other topics).
 */
class SearchController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    /**
     * Fetches the user's search item and uses it to compute
     * a search result.
     * @author Dominik Imsirovic
     */
    public function indexAction()
    {
         
        $form = new Application_Form_Search();
        $form->setAction($this->view->url());
        $this->view->form = $form;
        
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($_POST)) {
               $search_item = $_POST["search_item"];
               $this->view->searchItem = $search_item;
               $this->view->searchRes=Model_Search::simpleSearch($search_item);               
            }
        }
        
    }

    /**
     * Display the public view of a given user
     * @author  Dominik Imsirovic, Christophe Sourisse
     */
    public function userAction()
    {
        
        $session = Zend_Registry::get("my_session_namespace");
        
        
        if(isset($_GET["avatar"]))
       $avatar= $_GET["avatar"];
        else $avatar = $session->user["userName"];
 
       //data is used to decide which user data is displayed
        //by default the timeline is displayed
       if(isset($_GET["data"]))
       $this->view->data= $_GET["data"]; 
   else  $this->view->data="timeline";
   
   //Count is used to decide how much data should be displayed
   //(e.g. how many messages should be viewed)
   //by default 10 items are displayed
   if(isset($_GET["count"]))
       $this->view->count=$_GET["count"];
   else $this->view->count=10;
   
 
       
       // Ensure that the desired user isn't the current user
       // If yes, redirect to "/user/profile"
       
       /*if ($avatar == $session->user["userName"])
          $this->_redirect('/user/profile'); */
   
       //check if page belongs to the current user
        if ($avatar == $session->user["userName"])
         $currentUser=1;
        else $currentUser=0;
        
       // Nothing should be given to the view if the user isn't logged
       if (isset($session->user)) {
          
          // Initialize the array which will contain the forms to be displayed
          $this->view->forms = array();
          
          // Get data about the desired user
          $topic = Model_Users::getUserBySurname($avatar);
       
          //check if current page is a topic or a user
        $isTopic=$topic["isTopic"];
        
          
          // If the logged user has already subscribed to that topic or if the current
        //page is the user's own page
          if ($currentUser==1||Model_PublishSubscribe::hasAlreadySubscribed($session->user["topics"], $topic["id"])) {
             // ...add the form to publish a message
             $formPublish = new Application_Form_Publish(
                     "Publish a new post on that topic?",
                     "Publish!"
             );
             $formPublish->setAction("javascript:publish();");
             $this->view->forms[] = $formPublish;
             
             // ...add the form to unsubscribe to that topic(only if the page doesn't belong
             //to the current user!)
             
             if($currentUser==0){
             $formUnsubscribe = new Application_Form_Subscription(
                     'I don\'t want to follow this topic anymore!'
             );
             $formUnsubscribe->setAction("javascript:subscriptionChange(\"unsubscribe\");");
             $this->view->forms[] = $formUnsubscribe;
             }
          }
          // Else, if the logged user has not yet subscribed to that topic...
          else {
             // ...add the form to subscribe to that topic(only for visitors)
              
               if($currentUser==0){
                $form = new Application_Form_Subscription('Subscribe!');
                $form->setAction("javascript:subscriptionChange(\"subscribe\");");
                $this->view->forms[] = $form;
               }
          }
          
          // Give to the view the JS functions to publish & subscribe
          $this->view->headScript()->appendFile(
                  'http://localhost/WhatsUpProject/public/js/publishSubscribe.js',
                  'text/javascript'
          );
          
          
             if($isTopic==1){
              $label="topic";
          }else{
              $label="user";
          }
          
          //prevent XSS
          $topic["name"]=Model_Users::preventXSS($topic["name"]);
          $topic["date"]=Model_Users::preventXSS($topic["date"]);
          $topic["userName"]=Model_Users::preventXSS($topic["userName"]);
           $topic["description"]=Model_Users::preventXSS($topic["description"]);
           $topic["location"]=Model_Users::preventXSS($topic["location"]);
          
          // Pass to the view all other data to display
           $this->view->currentUser = $currentUser;
          $this->view->isTopic=$isTopic;
          $this->view->topic = $topic;
          $this->view->postsFromTopics =  Model_Home::getNewMessages_sorted($topic["topics"]);
          $this->view->postsFromVisitedUser =  Model_Home::getVisitorPosts_sorted($topic["id"]);
          $this->view->topics =  Model_Users::getUsers($topic["topics"]);
          $this->view->subscribers =  Model_PublishSubscribe::getSubscribers($topic["id"]);
          $this->view->session=$session;
          $this->view->label=$label;
          
       
          
          
       }
    }


}


